﻿using System.Collections.Generic;
using HVACWebService.Cloud.Able;
using HVACWebService.DataAccese.DataService;
using HVACWebService.DataAccese.Entity;

namespace HVACWebService.Business
{
    public class CustomerDeviceService
    {
        /// <summary>
        /// 绑定设备
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="pId"></param>
        /// <param name="name"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public int BindingDevice(string openId, string pId, string name, string phoneNumber, string address)
        {
            var ableDevice = new AbleDeviceCloudService();

            var isOnline = ableDevice.GetAbleOnlineDeviceInfo(pId);
            if (isOnline)
            {
                var isBinding = ableDevice.IsDeviceBound(pId);
                if (!isBinding)
                {
                    var isBinded = ableDevice.BindDevice(openId, pId, name);
                    if (isBinded != null)
                    {
                        CreateCustomerDevice(openId, pId, isBinded.DeviceId, name, phoneNumber, address, 1);
                        return 2;
                    }
                    else
                    {
                        return 3;
                    }
                }
                else
                {
                    return 1;
                }
            }

            return 0;
        }


        /// <summary>
        /// 解绑 able 设备绑定
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="physicalDeviceId"></param> 
        public bool UnbindDevice(string openId, string physicalDeviceId)
        {
            var ableDevice = new AbleDeviceCloudService();
            return ableDevice.UnbindDevice(openId, physicalDeviceId);
        }

        /// <summary>
        /// 新建用户绑定
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="deviceId"></param>
        /// <param name="ableDeivceId"></param>
        /// <param name="deviceName"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="address"></param>
        /// <param name="bindState"></param>
        /// <returns></returns>
        public int CreateCustomerDevice(string openId, string deviceId, string ableDeivceId, string deviceName,
            string phoneNumber, string address, int bindState)
        {
            var customerDeviceDataAccess = new CustomerDeviceDataAccess();
            return customerDeviceDataAccess.CreateCustomerDevice(openId, deviceId, ableDeivceId, deviceName, phoneNumber,
                address, bindState);
        }

        /// <summary>
        /// 更新绑定状态
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="deviceId"></param>
        /// <param name="bindState"></param>
        /// <param name="ableDeviceId"></param>
        /// <returns></returns>
        public int UpdateBindState(string openId, string deviceId, int bindState, string ableDeviceId)
        {
            var customerDeviceDataAccess = new CustomerDeviceDataAccess();
            return customerDeviceDataAccess.UpdateBindState(openId, deviceId, bindState, ableDeviceId);
        }

        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public CustomerDevice GetCustomerDeviceByOpenIdAndDeviceId(string openId, string deviceId)
        {
            var customerDeviceDataAccess = new CustomerDeviceDataAccess();
            return customerDeviceDataAccess.GetCustomerDeviceByOpenIdAndDeviceId(openId, deviceId);
        }

        /// <summary>
        /// 获取用户设备列表
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public List<CustomerDevice> GetCustomerDeviceByOpenId(string openId)
        {
            var customerDeviceDataAccess = new CustomerDeviceDataAccess();
            return customerDeviceDataAccess.GetCustomerDeviceByOpenId(openId);
        }

        /// <summary>
        /// 获取用户用户绑定设备信息
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public CustomerDevice GetCustomerDeviceBindByOpenIdAndDeviceId(string openId, string deviceId)
        {
            var customerDeviceDataAccess = new CustomerDeviceDataAccess();
            return customerDeviceDataAccess.GetCustomerDeviceBindByOpenIdAndDeviceId(openId, deviceId);
        }
    }
}